package com.example.utils;

import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.util.Log;

/**
 * 运行在刷量包进程
 *
 */
public class CrashHandler implements UncaughtExceptionHandler {
    private static final String TAG = "CrashHandler";
    private static CrashHandler sInstance = new CrashHandler();
    private UncaughtExceptionHandler mDefaultCrashHandler;
    private Context mContext;

    private CrashHandler() {
    }
 
    public static CrashHandler getInstance() {
        return sInstance;
    }
 
    public void init(Context context) {
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
    }
 
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
    	
//    	Zmlib.toastLong(mContext, "Crash");//无法弹出

		mContext.sendBroadcast(new Intent("com.edroid.intent.action.runapk"));
		Log.e(TAG,"刷量包的未捕获异常：");
		ex.printStackTrace();

        if (mDefaultCrashHandler != null) {
            mDefaultCrashHandler.uncaughtException(thread, ex);
        } else {
            Process.killProcess(Process.myPid());
        }
    }
}
